package com.wondersgroup.framework.widget.taglib.form;

import com.wondersgroup.framework.widget.annotations.WidgetTag;
import com.wondersgroup.framework.widget.annotations.WidgetTagAttribute;
import com.wondersgroup.framework.widget.context.DefaultSettings;
import com.wondersgroup.framework.widget.library.extjs.ExtComponent;
import com.wondersgroup.framework.widget.library.extjs.form.ExtGridSelect;
import com.wondersgroup.framework.widget.library.html.HtmlComponent;
import com.wondersgroup.framework.widget.library.html.Input;
import com.wondersgroup.framework.widget.taglib.form.util.FormUtils;
import com.wondersgroup.framework.widget.util.OutputUtils;
import com.wondersgroup.framework.widget.util.TagUtils;
import com.wondersgroup.framework.widget.validation.annotation.TypeValidate;

import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.jsp.JspException;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;

@WidgetTag(name = "gridSelect", tldTagClass = "com.wondersgroup.framework.widget.taglib.form.GridSelectTag", description = "gridSelect是一种下拉渲染出表格的特殊下拉框，目前支持表格的分页、输入字符自动查询联想等功能。")
public class GridSelectTag extends BaseSelectFieldTag {
	/**
	 * 添加固定版本号
	 */
	private static final long serialVersionUID = 1L;
	private int minChars = DefaultSettings.getInt("widget.gridselect.minChars");
	private String queryParam = "query";
	private String listWidth = "0";
	private int pageSize;
	private String totalSize;
	private String root;
	private String title;

	@TypeValidate
	private boolean autoComplete;
	private ExtGridSelect gridSelect;

	public int getMinChars() {
		return this.minChars;
	}

	@WidgetTagAttribute(description = "联想开始的最小字符数,默认为配置文件中的widget.gridselect.minChars值", type = "int")
	public void setMinChars(int paramInt) {
		this.minChars = paramInt;
	}

	public String getQueryParam() {
		return this.queryParam;
	}

	@WidgetTagAttribute(description = "进行远程调用时使用的查询域名称,默认为'query'")
	public void setQueryParam(String paramString) {
		this.queryParam = paramString;
	}

	public String getListWidth() {
		return this.listWidth;
	}

	@WidgetTagAttribute(description = "联想下拉表格宽度", type = "String")
	public void setListWidth(String paramInt) {
		this.listWidth = paramInt;
	}

	public int getPageSize() {
		return this.pageSize;
	}

	@WidgetTagAttribute(description = "分页的每页记录数,如果不设置此属性将不分页", type = "int")
	public void setPageSize(int paramInt) {
		this.pageSize = paramInt;
	}

	public String getTotalSize() {
		return this.totalSize;
	}

	@WidgetTagAttribute(description = "储存总记录数的json属性,在分页的情况下默认为'totalSize'")
	public void setTotalSize(String paramString) {
		this.totalSize = paramString;
	}

	public String getRoot() {
		return this.root;
	}

	@WidgetTagAttribute(description = "储存实际数据的json属性,在分页的情况下默认为'result'")
	public void setRoot(String paramString) {
		this.root = paramString;
	}

	public ExtGridSelect getGridSelect() {
		return this.gridSelect;
	}

	public void setGridSelect(ExtGridSelect paramExtGridSelect) {
		this.gridSelect = paramExtGridSelect;
	}

	public String getTitle() {
		return this.title;
	}

	@WidgetTagAttribute(description = "查询结果集标题")
	public void setTitle(String paramString) {
		this.title = paramString;
	}

	public boolean isAutoComplete() {
		return this.autoComplete;
	}

	@WidgetTagAttribute(description = "是否是自动完成框,默认为false", type = "boolean")
	public void setAutoComplete(boolean paramBoolean) {
		this.autoComplete = paramBoolean;
	}

	public void processStartTagContent() throws JspException {
		StringBuffer localStringBuffer = new StringBuffer();
		localStringBuffer.append(OutputUtils.writeJSFile(this.pageContext, new String[] { "wonders/form/wonders-form-min.js" }));
		if ((getToolBarArray() == null) && (getStatusBarArray() == null)) {
			Input localInput = new Input();
			localInput.setId(getId());
			localStringBuffer.append(OutputUtils.writeHTML(new HtmlComponent[] { localInput }));
		}
		this.gridSelect = new ExtGridSelect(getId());
		this.gridSelect.setHideTrigger(isAutoComplete());
		BeanUtils.copyProperties(this, this.gridSelect);
		this.gridSelect.setStore(this.gridSelect.getId() + "_store");
		this.gridSelect.setHiddenName(this.gridSelect.getProperty());
		if (!(this.gridSelect.isHideTrigger())) {
			this.gridSelect.setMinChars(0);
		}
		
		if (this.gridSelect.isReadOnly()) {
			this.gridSelect.setHideTrigger(true);
		}
			
		if ((getToolBarArray() == null) && (getStatusBarArray() == null)) {
			this.gridSelect.setApplyTo(this.gridSelect.getId());
		}
			
		this.gridSelect.setGridColumn(TagUtils.inGridColumn(this.pageContext, this.gridSelect));
		if (!(this.gridSelect.isGridColumn()))
			TagUtils.getInstance().write(this.pageContext, localStringBuffer.toString());
	}

	@SuppressWarnings("rawtypes")
	public void processEndTagContent() throws JspException {
		StringBuffer localStringBuffer = new StringBuffer();
		localStringBuffer.append("new Ext.XTemplate(");
		String tableWidth = (StringUtils.isEmpty(gridSelect.getWidth()) || "0".equals(gridSelect.getWidth())) ? "120" : gridSelect.getWidth();
		if (StringUtils.isNotEmpty(gridSelect.getListWidth()) && !("0".equals(gridSelect.getListWidth()))) {
			tableWidth = gridSelect.getListWidth();
		}
		
		localStringBuffer.append("'<table cellspacing=0 cellpadding=0 width=" + (tableWidth.contains("%") ? tableWidth: (Integer.parseInt(tableWidth) - 2)) + " class=gridSelect-table>");
		
		if (this.gridSelect.getTitle() != null) {
			localStringBuffer.append("<caption class=gridSelect-caption><b>" + this.gridSelect.getTitle() + "</b></caption>");
		}
			
		ArrayList localArrayList = (ArrayList) this.pageContext.getAttribute("wonders.gridSelect.column");
		localStringBuffer.append("<tr height=25>");
		localStringBuffer.append("<td class=gridSelect-title>");
		Iterator localIterator1 = localArrayList.iterator();
		Object localObject = null;
		int xx = 0;
		while (localIterator1.hasNext()) {
			xx++;
			localObject = (GridSelectColumnTag) localIterator1.next();
			if(((GridSelectColumnTag) localObject).getWidth().contains("%")) {
				localStringBuffer.append("<span style=\"width:"
						+ ((GridSelectColumnTag) localObject).getWidth()
						+ ((xx==localArrayList.size()) ? (";\" class=gridSelect-header-last><b>"):(";\" class=gridSelect-header><b>"))
						+ ((GridSelectColumnTag) localObject).getHeader()
						+ "</b></span>");
			} else {
				localStringBuffer.append("<span style=\"width:"
						+ ((GridSelectColumnTag) localObject).getWidth()
						+ ((xx==localArrayList.size()) ? ("px;\" class=gridSelect-header-last><b>") : ("px;\" class=gridSelect-header><b>"))
						+ ((GridSelectColumnTag) localObject).getHeader()
						+ "</b></span>");
			}
		}
		localStringBuffer.append("</td>");
		localStringBuffer.append("</tr>");
		localStringBuffer.append("</table>',");
		localStringBuffer.append("'<div style=\"height:110\"><tpl for=\".\"><div class=search-item>',");
		localStringBuffer.append("'");
		int j = 0;
		localObject = new StringBuffer();
		Iterator localIterator2 = localArrayList.iterator();
		int yy = 0;
		while (localIterator2.hasNext()) {
			yy++;
			GridSelectColumnTag localGridSelectColumnTag = (GridSelectColumnTag) localIterator2.next();
			if(localGridSelectColumnTag.getWidth().contains("%")){
				localStringBuffer.append("<span style=\"width:" + localGridSelectColumnTag.getWidth() + ";\"");
			} else {
				localStringBuffer.append("<span style=\"width:" + localGridSelectColumnTag.getWidth() + "px;\"");
			}
			
			if (StringUtils.isNotEmpty(localGridSelectColumnTag.getRendererData())) {
				j = 1;
				((StringBuffer) localObject).append("<script>var "
						+ localGridSelectColumnTag.getId() + "dicData = "
						+ localGridSelectColumnTag.getRendererData()
						+ ";</script>");
				localStringBuffer
						.append(((yy==localArrayList.size()) ? ("class=gridSelect-content-last") : ("class=gridSelect-content")) + ">{[this.transformDicData(values."
								+ localGridSelectColumnTag.getProperty()
								+ ","
								+ localGridSelectColumnTag.getId()
								+ "dicData)]}</span>");
			} else {
				localStringBuffer.append(((yy==localArrayList.size()) ? ("class=gridSelect-content-last") : ("class=gridSelect-content")) + ">{" + localGridSelectColumnTag.getProperty() + "}</span>");
			}
		}
		localStringBuffer.append("',");
		localStringBuffer.append("'</div></tpl></div>'");
		if (j != 0) {
			localStringBuffer
					.append(",{transformDicData : function(v, dicData) {if (dicData instanceof Array) {for (var i = 0; i < dicData.length; i++) {if (dicData[i] instanceof Array && dicData[i].length > 1) {if (v == dicData[i][0]) {return dicData[i][1];}} else if (dicData[i] instanceof Object) {for (var key in dicData[i]) {if (key == v) {return dicData[i][key];}}}}}return v;}}");
		}
		localStringBuffer.append(")");
		this.gridSelect.setTpl(localStringBuffer.toString());
		TagUtils.getInstance().write(this.pageContext, ((StringBuffer) localObject).toString());
		localObject = new StringBuffer();
		((StringBuffer) localObject).append(OutputUtils.ExtOnReady(new ExtComponent[] { this.gridSelect }));
		if (!(this.gridSelect.isGridColumn())) {
			FormUtils.addFieldsToForm(this.pageContext, this.gridSelect, getDisplayField());
			TagUtils.getInstance().write(this.pageContext, ((StringBuffer) localObject).toString());
		}
		this.pageContext.removeAttribute("wonders.gridSelect.column");
	}
}
